package interactive.focusPhoto 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	import ginger.controller.AutoPlayController;
	import ginger.geom.RectangleShape;
	import ginger.utils.ObjectPool;
	import interactive.focusPhoto.item.PhotoItem;
	/**
	 * 焦点图
	 * @author Ning
	 */
	public class FocusPhoto extends Sprite
	{
		//子元素类
		protected var itemClass:Class;
		//子元素对象池
		protected var itemObjectPool:ObjectPool;
		//子元素集合
		protected var items:Array;
		//数据
		protected var data:Array;
		//延迟
		private var _delay:Number;
		//索引号
		protected var index:int;
		//元素持有者
		protected var holder:Sprite;
		//持有者遮罩
		protected var holderMask:RectangleShape;
		//元素宽
		protected var itemWidth:Number;
		//元素高
		protected var itemHeight:Number;
		
		private var _mouseWheel:Boolean;
		
		
		//自动播放控制器
		protected var autoPlayController:AutoPlayController;
		
		public function FocusPhoto(itemClass:Class=null,itemWidth:Number=100,itemHeight:Number=100,delay:Number=5000,mouseWheel:Boolean=false) 
		{
			if (itemClass == null)
			    this.itemClass = PhotoItem;
			else {
				var item:*= new itemClass();
				if (item is PhotoItem)
				    this.itemClass = itemClass;
				else {
					throw new Error('类型错误');
					return;
				}
			}
		    itemObjectPool = new ObjectPool(this.itemClass);
			items = [];
			autoPlayController = new AutoPlayController(this, delay, next);
			holder = new Sprite();
			addChild(holder);
			holderMask = new RectangleShape();
			addChild(holderMask);
			holder.mask = holderMask;
			this.itemWidth = itemWidth;
			this.itemHeight = itemHeight;
			this.mouseWheel = mouseWheel;
			setSize(itemWidth, itemHeight);
		}
		/**
		 * 设置尺寸
		 * @param	width
		 * @param	height
		 */
		public function setSize(width:Number, height:Number):void
		{
			holderMask.updateGraphics(width, height);
		}
		/**
		 * 设置数据
		 * @param	data
		 */
		public function setData(data:Array):void
		{
			this.data = data;
		}
		/**
		 * 抽象创建
		 */
		public function create():void
		{
			throw new Error('此方法为抽象方法，不能单独运行');
		}
		/**
		 * 自动播放
		 * @param	delay
		 */
		public function play():void
		{
			autoPlayController.play();
		}
		/**
		 * 停止
		 */
		public function stop():void
		{
			autoPlayController.stop();
		}
		/**
		 * 所有子元素同时开始载入
		 */
		public function loadAll():void
		{
			for (var i:int = 0; i < items.length; i++ )
			{
				var item:PhotoItem = items[i] as PhotoItem;
				item.load();
			}
		}
		/**
		 * 顺序载入所有
		 */
		public function sortLoadAll():void
		{
			var index:int = 0;
			load(index);
			function load(i:int):void
			{
				var item:PhotoItem = items[i] as PhotoItem;
				if (!item.isLoad) {
					item.addEventListener(Event.COMPLETE, onComplete);
					item.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
					item.load();
				}
				else 
				    onComplete();
			}
			function onIoError(evt:Event):void
			{
				evt.target.removeEventListener(Event.COMPLETE, onComplete);
				evt.target.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
				next();
			}
			function onComplete(evt:Event=null):void
			{
				evt.target.removeEventListener(Event.COMPLETE, onComplete);
				evt.target.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
				next();
			}
			function next():void
			{
				index++;
				if (index < count)
				    load(index);
			}
		}
		/**
		 * 上一个
		 */
		public function prev():void
		{
			index--;
			if (index < 0 )
			    index = count - 1;
			showAt(index);
		}
		/**
		 * 下一个
		 */
		public function next():void
		{
			index++;
			if (index > count - 1)
			    index = 0;
			showAt(index);
		}
		/**
		 * 展示指定的索引
		 * @param	index
		 */
		public function showAt(index:int):void
		{
			if (this.index == index)
			    return;
			this.index = index;
		}
		/**
		 * 移除所有
		 */
		protected function removeAll():void
		{
			while (items.length > 0) {
	    		var item:PhotoItem = items.pop() as PhotoItem;
	    		removeObject(item);
	    	}
		}
		/**
		 * 移除指定索引的元素
		 * @param	index
		 */
		protected function removeAt(index:int):void
		{
			var item:PhotoItem = items[index] as PhotoItem;
			removeObject(item);
		}
		/**
		 * 移除指定的元素对象
		 * @param	item
		 */
		protected function removeObject(item:PhotoItem):void
		{
			if (holder.contains(item)) {
	    		holder.removeChild(item);
	    	}
	    	var index:int = items.indexOf(item);
	    	if (index >= 0) {
	    		items.splice(index, 1);
	    	}
			item.die();
			itemObjectPool.returnObject(item);
		}
		
		/**
		 * 返回指定索引的元素
		 * @param	index
		 * @return
		 */
		protected function getItemAt(index:int):PhotoItem
		{
			return items[index] as PhotoItem;
		}
		
		/**
		 * 获取子元素
		 * @return
		 */
	    protected function getItem():PhotoItem {
		    var item:PhotoItem = itemObjectPool.borrowObject() as PhotoItem;
			item.init();
			item.setSize(itemWidth, itemHeight);
			return item;
		}
		
		public function get delay():Number 
		{
			return _delay;
		}
		
		public function set delay(value:Number):void 
		{
			_delay = value;
			autoPlayController.delay = _delay;
		}
		
		public function get count():int {
			return data.length;
		}
		
		public function set mouseWheel(value:Boolean):void 
		{
			_mouseWheel = value;
			if (_mouseWheel) {
				if(!hasEventListener(MouseEvent.MOUSE_WHEEL))
				    addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			}
			else {
				if(hasEventListener(MouseEvent.MOUSE_WHEEL))
				    removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			}
		}
		
		//鼠标滚轮
		private var iswheeling:Boolean;
		private function onMouseWheel(evt:MouseEvent):void
		{
			if (iswheeling)
			    return;
			iswheeling = true;
			setTimeout(function ():void { iswheeling = false }, 100);
			if (evt.delta < 0)
			    next();
			else 
			    prev();
		}
	}
}